<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "j_log_api".
 *
 * @property string $id
 * @property integer $ip
 * @property string $ip_addr
 * @property string $url
 * @property string $action_v
 * @property integer $source_type
 * @property string $action_name
 * @property string $action_fun
 * @property string $action_param
 * @property string $device
 * @property string $extend1
 * @property string $extend2
 * @property string $extend3
 * @property string $extend4
 * @property integer $extend5
 * @property integer $extend6
 * @property string $extend7
 * @property string $extend8
 * @property string $extend9
 * @property string $extend10
 * @property string $creator
 * @property string $create_time
 */
class LogApi extends \yii\db\ActiveRecord
{
    const SOURCE_TYPE_ONE = 1;  //来源类型1：商品同步

    public $aSourceTypeOne = [
        1=>'商品同步'
    ];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'j_log_api';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ip', 'source_type', 'extend5', 'extend6'], 'integer'],
            [['extend3', 'extend4', 'create_time'], 'safe'],
            [['extend7', 'extend8'], 'number'],
            [['ip_addr', 'action_name'], 'string', 'max' => 100],
            [['url'], 'string', 'max' => 255],
            [['action_v'], 'string', 'max' => 2],
            [['action_fun'], 'string', 'max' => 50],
            [['action_param', 'action_return_param','extend1', 'extend2'], 'string', 'max' => 2000],
            [['device'], 'string', 'max' => 20],
            [['extend9', 'extend10'], 'string', 'max' => 5000],
            [['creator'], 'string', 'max' => 60],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '自增长',
            'ip' => 'ip地址',
            'ip_addr' => 'ip所属地',
            'url' => '接口地址',
            'action_v' => '控制器版本1：默认接口版本',
            'source_type' => '来源类型1：商品同步',
            'action_name' => '控制器名称',
            'action_fun' => '控制器方法',
            'action_param' => '请求参数',
            'action_return_param' => '返回参数',
            'device' => '设备',
            'extend1' => '扩展1',
            'extend2' => '扩展2',
            'extend3' => '扩展3',
            'extend4' => '扩展4',
            'extend5' => '扩展5',
            'extend6' => '扩展6',
            'extend7' => '扩展7',
            'extend8' => '扩展8',
            'extend9' => '扩展9',
            'extend10' => '扩展10',
            'creator' => '创建人',
            'create_time' => '创建时间',
        ];
    }
    public function beforeSave($insert){
        $this->ip   = ip2long(Yii::$app->request->getUserIP());
        $this->creator          = 'sys';
        $this->create_time      = date('Y-m-d H:i:s');
        return true;
    }

    /**
     * @param 记录同步商品日志信息
     * @param array $returnParam
     * @param return turn
     */
    public static function  keepSave($returnParam=[]){

        //记录错误日志
        $oLogApi                    = new static();
        $oLogApi->url               = Yii::$app->request->getHostInfo();
        $oLogApi->action_v         = Yii::$app->controller->module->id;
        $oLogApi->action_name      = Yii::$app->controller->id;
        $oLogApi->action_fun       = Yii::$app->controller->action->id;
        $oLogApi->action_param     = json_encode( Yii::$app->request->post());
        $oLogApi->source_type      = self::SOURCE_TYPE_ONE;
        $oLogApi->action_return_param      =json_encode($returnParam);

        if(!$oLogApi->save())
            Yii::setLogger('请求接口日志日志保存错误');
    }
}
